<?php
/**
 * TestWuController.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/11/6- 5:41 下午
 */

namespace console\controllers;


use api\dao\ComProductsDao;
use api\dao\ComProductsDetailDao;
use api\models\ComProducts;
use api\models\ComProductsDetail;
use api\services\CloudProductService;
use api\services\SyncEsService;
use api\task\SyncProductList;
use common\components\api\isv\Goods;
use common\helpers\ImagesHelper;
use common\helpers\IpHelper;
use common\helpers\StringHelper;
use common\models\com\ComSpider;
use common\models\es\EsComProducts;
use common\models\es\EsCopyProducts;
use common\models\es\EsPddMallProducts;
use console\components\helpers\ConsoleHelper;
use GuzzleHttp\Tests\Stream\Str;
use yii\base\Exception;
use yii\console\Controller;
use yii\helpers\ArrayHelper;
use Yii;
use yii\helpers\Json;

class TestWuController extends Controller
{


    /**
     * 初始化es
     */
    public function actionInitEs()
    {
        $esProducts = new EsComProducts();

        $resultProduct =  $esProducts->initIndex(false);

        $esMallProducts = new EsPddMallProducts();

        $resultMallProducts =  $esMallProducts->initIndex(false);

        $esCopyProducts = new EsCopyProducts();

        $resultCopyProducts = $esCopyProducts->initIndex(false);

        print_r($resultProduct);
        print_r($resultMallProducts);
        print_r($resultCopyProducts);


    }


    public function actionTest()
    {

//        (new TestService())->test();




    }


    public function actionSyncCopyProduct()
    {
        /**
         * 云库es
         */
        $esPddMallProducts = new EsPddMallProducts();

        $resultEs =  $esPddMallProducts->initIndex(true);

        print_r($resultEs);



        /**
         * pdd_copy_product
         */
        $syncEsService = new SyncEsService();

        $ids =[];
        for ($i = 20000; $i >= 1; $i--) {
            array_push($ids, $i);
            if ($i % 100 == 0) {
                echo Json::encode($ids) . PHP_EOL;
                $result = $syncEsService->syncCopyProducts($ids);
                var_dump($result);
                $ids = [];
            }
        }


    }



    public function actionEs()
    {

        /**
         * 云库es
         */
        $esProducts = new EsComProducts();

        $resultEs =  $esProducts->initIndex(true);

        print_r($resultEs);

//

        /**
         * 店铺商品es
         */
//        $esMallProduct = new EsTbMallProducts();
//
//        $resultEs = $esMallProduct->initIndex(true);
//
//        print_r($resultEs);


        /**
         * 同步日志es
         */
//        $esCopyProducts = new EsCopyProducts();
//
//        $resultEs = $esCopyProducts->initIndex(true);
//
//        print_r($resultEs); exit;
//
//        $esMallProducts = new EsTbMallProducts();
//
//        $resultMallProducts =  $esMallProducts->initIndex(true);
//
//        var_dump($resultMallProducts);
//
//        $syncEsService = new SyncEsService();
//        $syncEsService->syncCopyProducts([242]);
        $syncEsService = new SyncEsService();
//
//
        $ids =[];
        for ($i = 3000; $i >= 1; $i--) {
            array_push($ids, $i);
            if ($i % 10 == 0) {
                echo Json::encode($ids) . PHP_EOL;
                $result = $syncEsService->syncCloudProduct($ids);
                var_dump($result);
                $ids = [];
            }
        }
    }



    public function actionProductDetail()
    {
        $details = ComProductsDetail::find()->orderBy('product_id ASC')->all();
        echo "<pre>";
        print_r($details);
        exit();
        $allowImgExt = ['jpg','png','JPG','PNG'];
        foreach($details as $k => $detail)
        {

            $pattern = "/\<[img|IMG].*?src=['|\"](.*?(?:[.gif|.jpg]))['|\"].*?[\/]?>/i";
            preg_match_all($pattern,$detail->pc_desc,$matchImg);


            $detail_gallery = [];
            if(isset($matchImg[1]) && !empty($matchImg[1]))
            {
                foreach($matchImg[1] as $img)
                {
                    $tmpImg = explode('.',$img);
                    $ext = end($tmpImg);
                    if(!in_array($ext,$allowImgExt)) continue;
                    array_push($detail_gallery,$img);
                }
            }


            if(count($detail_gallery))
            {
                $detail->detail_gallery = StringHelper::jsonEncode($detail_gallery);
            }

            //剔除描叙中的特殊符号、空格、换行等
            $desc = str_replace(["\\",'"',"\r\n","\n"," ","&nbsp;"],'', strip_tags($detail->pc_desc));

            if(!empty($desc))
            {
                $detail->goods_desc = trim($desc);
            }

            $result = $detail->save();

            if($result)
            {
                echo $detail->product_id.'--更新成功'.PHP_EOL;
            }
        }
    }





    public function actionCopy()
    {
        list($count,$list) = (new CloudProductService())->getListPage(1,200,[]);

        $comProductService = new ComProductsDao();
        $goodsIsv = new Goods();
        foreach($list as $k => $item)
        {
            $mappingCats  = $goodsIsv->pddGoodsOuterCatMappingGet([
                'outer_cat_id' => $item['cid'],
                'outer_cat_name' => '',
                'outer_goods_name' => $item['title'],
            ],768331977);
            $catId =0;
            if(isset($mappingCats['cat_id3']) && $mappingCats['cat_id3'] != 0)
            {
                $catId = $mappingCats['cat_id3'];
            }
            if(isset($mappingCats['cat_id4']) && $mappingCats['cat_id4'] != 0)
            {
                $catId = $mappingCats['cat_id4'];
            }

            $ComProducts = ComProducts::findOne(['id'=>$item['id']]);
            $comProductService->updateData($ComProducts,['cid'=>$catId]);
            ConsoleHelper::runInfo($item['title'].'->类目变更为->'.$catId);
        }

    }

}
